package library;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class CommonLibrary{
	
	private WebDriver driver;
	public CommonLibrary(WebDriver _driver){
		driver = _driver;
		
	}
public int searchText(String text, int colno)
	{
		boolean flag = false;
		String locator;
		int i=-1;
		int pageCount = getPages();
		for(int j=1;j <= pageCount; j++)
		{
			int rowCount = getRows();
			for (i = 1; i <= rowCount; i++) 
			{
				try {
					locator = "//tr["+i+"]/td["+colno+"]/div";
						
					if (text == driver.findElement(By.xpath(locator)).getText())
					{
						flag = true;
						break;
					}
				}catch (Exception $e) {}
			}
			if(flag != true)
			{
				driver.findElement(By.cssSelector("css=div.pNext.pButton")).click();
			}
			else
			break;
		}
		if(flag == true)
		return (i);
		else
		return (-1);

	}
	
	
	
	private int getRows() {
		return driver.findElements(By.xpath("//table[@id='flex1']/tbody/tr")).size();
  }
	
	private int getPages() {
		return Integer.parseInt(driver.findElement(By.xpath("//article[@id='content']/div[2]/section//span/span")).getText());
   }
	
	
	public void onRow(int rowid,String action)    
	{
		
		if (action == "edit" || action == "Edit")
		{
			
		 driver.findElement(By.xpath("//a[contains(text(),'Edit')]["+rowid+"]")).click();
		   
		}
	
		if(action == "active" || action == "Active")
		 {
			if(driver.findElement(By.xpath("//tr["+rowid+"]/td[7]/div/a")).getText() =="Active")
			 driver.findElement(By.xpath("//tr["+rowid+"]/td[7]/div/a")).click();
		 }
		 if(action == "Inactive" || action == "inactive"){
			 
			 if(driver.findElement(By.xpath("//tr["+rowid+"]/td[7]/div/a")).getText() =="Inactive")
			 driver.findElement(By.xpath("//tr["+rowid+"]/td[7]/div/a")).click();
		 }
	}
}